package com.wangrui.test;

import java.io.IOException;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.wangrui.dao.ClassMapper5;
import com.wangrui.dao.UserMapper3;
import com.wangrui.entity.Class5;
import com.wangrui.entity.User;

public class MyBatisTest05 {

    /**
     * 获得MyBatis SqlSessionFactory
     * SqlSessionFactory负责创建SqlSession，一旦创建成功，就可以用SqlSession实例来执行映射语句
     * ，commit，rollback，close等方法。
     *
     * @return
     */
    private static SqlSessionFactory getSessionFactory() {
        SqlSessionFactory sessionFactory = null;
        String resource = "configuration.xml";
        try {
            sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader(resource));
        } catch (IOException e) {

            e.printStackTrace();
        }
        return sessionFactory;
    }

    /**
     * 一对多查询
     */
    @Test
    public void testSelect() {
        SqlSessionFactory factory = getSessionFactory();
        //默认是手动提交的
        SqlSession session = factory.openSession();
        ClassMapper5 classMapper = session.getMapper(ClassMapper5.class);
        Class5 class1 = classMapper.findById(1);
        System.out.println(class1);

        Class5 class2 = classMapper.findById2(1);
        System.out.println(class2);

        Class5 class3 = classMapper.findById3(1);
        System.out.println(class3);

        Class5 class4 = classMapper.findById4(2);
        System.out.println("class4:" + class4);
        session.close();
    }
}
